const Router = require('koa-router')
const AuthorizeController = require('../app/controllers/oauth/AuthorizeController')
const OauthClientController = require('../app/controllers/oauth/OauthClientController')
const AdminController = require('../app/controllers/rbac/AdminController')
const oauth = require('../app/middlewares/oauth')
class Oauth {
    constructor() {
        this.$oauth = new Router()
        this.$oauth.use(oauth)
        this.init()
        this.routes()
        return this.$oauth
    }

    /**
     * 初始化路由 加载资源路由
     * 
     * @memberof Api
     */
    async init () {
        await this.resource('oauth/client', OauthClientController)
        await this.resource('oauth/admin', AdminController)
    }
    // 定义普通路由
    async routes() {
        this.$oauth.post('/authorize/token', AuthorizeController.token)
        this.$oauth.post('/authorize/user', AuthorizeController.user)
        this.$oauth.get('/authorize/code',  AuthorizeController.code)
    }

    /**
     * 生成资源路由
     * 
     * @param {any} name 
     * @param {any} controller 
     * @memberof Api
     */
    async resource (name, controller) {
        this.$oauth.get(`/${name}`, controller.index)
        this.$oauth.get(`/${name}/create`,  controller.create)
        this.$oauth.post(`/${name}`,  controller.store)
        this.$oauth.get(`/${name}/:id`,  controller.show)
        this.$oauth.get(`/${name}/:id/edit`,  controller.edit)
        this.$oauth.post(`/${name}/:id`,  controller.save)
        this.$oauth.get(`/${name}/:id/destroy`,  controller.destroy)
    }
    
}

module.exports = new Oauth()
